var state=0;
var win=5;
$(function() {
					navigate_tabs(5);
})

function navigate_tabs(num) {
	for (var i = 1; i <= 5; i++) {
	  $("#tab" + i).removeClass('buttonHover');
	}
	$("#tab" + num).addClass('buttonHover');
	win=num;
	if(num==1){
		state=10;
	}else if(num==2){
		state=30;
	}else if(num==3){
		state=31;
	}else if(num==4){
		state=40;
	}else if(num==5){
		state=0;
	}
	$("#page").myPagination(
			{
				currPage : 1,
				pageCount : 10,
				pageSize : 10,
				ajax : {
					on : true,
					callback : 'change',
					url : appPath
							+ "/buyer/getBuyer.htm?token="
							+ $.cookie('token')+"&state="+state,
					dataType : 'html'
				}
			});
//	$("." + container).show('slow');
}
function change(data) {
	$('#list').html(data);
	if($('#pageCount').val()==1||($('#pageCount').val()==0)){
		$('#page').hide();
	}else{
		$('#page').show();
	}
	if(($('#list div').length==0)&&($('.current').text()>1)){
		refresh(1);
	}
	for(var i=1;i<6;i++){
		if(win==i&&$('#list div').length==0){			
			$('#img'+i).show();
		}else{
			$('#img'+i).hide();
		}
	}
	$('.noCancel').parent().children().children('.cancel').remove();
}
function gotoBuy() {
	gotoHtml('/index.htm');
}

function payOrder(code) {
	gotoHtml('/pay/payPage.htm?code=' + code);
}

function doComment(id) {
	gotoHtml('/comments/comment.htm?goodsOrderId=' + id);
}

function cancelOrder(id) {
	myconfirm("确定要取消订单吗？", function() {
		showLoading();
		$.ajax({
			url : appPath + "/order/cancelOrder.json",
			dataType : 'json',
			type : 'post',
			data : {
				token : $.cookie("token"),
				orderId : id
			},
			success : function(data) {
				hideLoading();
				if (data.code == -1) {
					hideLoading();
					myalert("请登录", function() {
						window.location = appPath + "/member/login.htm";
					})
					return;
				}
				if (data.code == 0) {
					hideLoading();
					myalert(data.msg);
					return;
				} else {
					myalert(data.msg,refresh);
				}

			},
			error : function() {
				hideLoading();
				myalert("系统异常，请重试");
			}
		})
	})
}
function refresh(num){
	if(num==''||num==null){
		num=0;
	}
	$("#page").myPagination(
			{
				currPage : $('.current').text()-num,
				pageCount : 10,
				pageSize : 10,
				ajax : {
					on : true,
					callback : 'change',
					url : appPath
							+ "/buyer/getBuyer.htm?token="
							+ $.cookie('token')+"&state="+state,
					dataType : 'html'
				}
			});
}

function cancelPayOrder(id) {
	myconfirm("确定要取消订单吗？", function() {
		showLoading();
		$.ajax({
			url : appPath + "/order/cancelPayOrder.json",
			dataType : 'json',
			type : 'post',
			data : {
				token : $.cookie("token"),
				orderId : id
			},
			success : function(data) {
				hideLoading();
				if (data.code == -1) {
					hideLoading();
					myalert("请登录", function() {
						window.location = appPath + "/member/login.htm";
					})
					return;
				}
				if (data.code == 0) {
					hideLoading();
					myalert(data.msg);
					return;
				} else {
					myalert(data.msg,refresh);
				}

			},
			error : function() {
				hideLoading();
				myalert("系统异常，请重试");
			}
		})
	})
}

function beforeReceipt(id) {
	
	$("#cover").show();
	$("#balancePopup").show();
	tc_center_balance();
	$("#guanbi").unbind('click');
	$("#queding").unbind('click');
	$("#guanbi").click(function(){
		$("#cover").hide();
		$("#balancePopup").hide();
	})	
	$("#queding").click(function(){
		$("#cover").hide();
		$("#balancePopup").hide();
		receipt(id);
		return;
	})	
}

function tc_center_balance(){
	var _top=(screen.height-$("#balancePopup").height())/3;
	var _left=(screen.width-$("#balancePopup").width())/2;
	
	$("#balancePopup").css({top:_top,left:_left});
}

function receipt(id) {
	var pass = $('#password').val();
	if (pass.length < 1) {
		myalert("请输入支付密码");
		return;
	}
		showLoading();
		$.ajax({
			url : appPath + "/order/receiptStorageOrder.json",
			dataType : 'json',
			type : 'post',
			data : {
				token : $.cookie("token"),
				storageOrderId : id,
				payPass:$.md5(pass + $('#key').val())
			},
			success : function(data) {
				hideLoading();
				if (data.code == -1) {
					hideLoading();
					myalert("请登录", function() {
						window.location = appPath + "/member/login.htm";
					})
					return;
				}
				if (data.code == 0) {
					hideLoading();
					myalert(data.msg);
					return;
				} else {
					myalert(data.msg,refresh);
				}

			},
			error : function() {
				hideLoading();
				myalert("系统异常，请重试");
			}
		})
}
